package com.cmall.common.foundation.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.cmall.common.foundation.entity.HttpClientEntity;

public class PostRequest{
	protected String responseData;
    protected HttpClientEntity client = new HttpClientEntity();
    protected String responseStatus;
    protected String encoding = "UTF-8";
    protected Integer socketTimeout = 20000;
    protected Integer connectTimeout = 20000;
    protected String uri;
    private static Log logger = LogFactory.getLog(PostRequest.class);
    
    public PostRequest() {
    	
    }
    public PostRequest(HttpClientEntity client) {
    	this.client = client;
    }

    public String postData() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(this.client.getPostUrl().toString());
        Iterator uefEntity;
        if (this.client.getHeader() != null && this.client.getHeader().size() > 0) {
            Map<String, String> map = this.client.getHeader();
            uefEntity = map.keySet().iterator();

            while(uefEntity.hasNext()) {
                String key = (String)uefEntity.next();
                httppost.setHeader(key, (String)map.get(key));
            }
        }

        List<NameValuePair> formParams = new ArrayList();
        uefEntity = this.client.getParams().entrySet().iterator();

        while(uefEntity.hasNext()) {
            Entry<String, Object> entry = (Entry)uefEntity.next();
            if (entry.getValue() instanceof String) {
                formParams.add(new BasicNameValuePair((String)entry.getKey(), entry.getValue().toString()));
            }
        }

        uefEntity = null;
        CloseableHttpResponse response = null;

        try {
            UrlEncodedFormEntity urlEncodedEntity = new UrlEncodedFormEntity(formParams, this.encoding);
            httppost.setEntity(urlEncodedEntity);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.socketTimeout).setConnectTimeout(this.connectTimeout).build();
            httppost.setConfig(requestConfig);
            logger.info("Executing Request " + httppost.getURI());
            logger.info("Executing Request " + EntityUtils.toString(httppost.getEntity()));
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                logger.info("--------------------------------------------- start");
                this.responseStatus = response.getStatusLine().getStatusCode() + "";
                logger.info("Response Status: " + this.responseStatus);
                this.responseData = EntityUtils.toString(entity, this.encoding);
                logger.info("Response Content: " + this.responseData);
                logger.info("--------------------------------------------- end");
            }
        } catch (UnsupportedEncodingException var26) {
            var26.printStackTrace();
        } catch (ClientProtocolException var27) {
            var27.printStackTrace();
        } catch (IOException var28) {
            var28.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException var25) {
                var25.printStackTrace();
            }

            try {
                httpclient.close();
            } catch (IOException var24) {
                var24.printStackTrace();
            }

        }

        return this.responseData;
    }
}
